package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.VolleyResponse;
import com.isunland.managesystem.entity.SuccessMessage;
import com.isunland.managesystem.entity.TravelListContent;
import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.utils.MyDateUtil;
import com.isunland.managesystem.utils.MyUtils;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.Date;
import java.util.HashMap;
import org.json.JSONException;

public class EditTravelReimburseListDetailFragment extends TravelReimburseListDetailFragment {
  public static final String EXTRA_TRAVELLIST_CONTENT =
      "com.isunland.managesystem.ui.EXTRA_TRAVELLIST_CONTENT";

  //private static final int REQUEST_DELETE = 0;
  //private static final int REQUEST_ALTER = 1;// 修改
  private TravelListContent mTravelListContent;

  // 创建菜单
  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);

    mTravelListContent =
        (TravelListContent) getArguments().getSerializable(EXTRA_TRAVELLIST_CONTENT);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mActivity = (BaseVolleyActivity) getActivity();
    mTravelListContent =
        (TravelListContent) getArguments().getSerializable(EXTRA_TRAVELLIST_CONTENT);
    //LogUtil.i(mTravelListContent.toString());
    //LogUtil.i(mTravelListContent.getARRIVE_PLACE());
  }

  // 初始化控件
  protected void initData() {

    // 详情
    etstartDate.setText(mTravelListContent.getLEAVE_TIME());
    etstartEar.setText(mTravelListContent.getLEAVE_PLACE());
    etendDate.setText(mTravelListContent.getARRIVE_TIME());
    etendEar.setText(mTravelListContent.getARRIVE_PLACE());
    tvtraftool.setText(mTravelListContent.getTRAFTOOL());
    ettraftoolCost.setText(mTravelListContent.getTRAFTOOL_COST());
    tvallStandard.setText(mTravelListContent.getTRAVEL_OFFSET());
    etstandard.setText(mTravelListContent.getGRANT_STANDARD());
    etcityCost.setText(mTravelListContent.getCITY_TRAFIC_FEE());
    ettravelDays.setText(mTravelListContent.getTOFFSET_DAYS());
    etnbuyCost.setText(mTravelListContent.getTOOL_OFFSET());
    etliveCost.setText(mTravelListContent.getHOTEL_FEE());
    etotherCost.setText(mTravelListContent.getOTHER_FEE());
    etPages.setText(mTravelListContent.getTCOST_BILLNUM());
    tvbillFee.setText(mTravelListContent.getCOST_SUBTOTAL());
    etcertificateNo.setText(mTravelListContent.getTICKETS_NUM());
    etNote.setText(mTravelListContent.getNOTE());
    tvallCost.setText(mTravelListContent.getSUM_BILL_FEE());
    tvInvestigateContent.setText(mTravelListContent.getINVESTIGATE_CONTENT());
    mId = mTravelListContent.getTRAFTOOLID();
    mName = mTravelListContent.getTRAFTOOL();
  }

  // 菜单点击事件
  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;
      case R.id.menu_item_confirm:
        boolean allEmpty = allEmpty();
        if (allEmpty) {
          Toast.makeText(getActivity(), R.string.nothing, Toast.LENGTH_SHORT).show();
          return true;
        }
        if (!TextUtils.isEmpty(etstartDate.getText().toString()) && !TextUtils.isEmpty(
            etendDate.getText().toString())) {
          String start = etstartDate.getText().toString();
          String end = etendDate.getText().toString();
          Date startDate = MyDateUtil.stringToDate(start, MyDateUtil.TYPE_MINITE);
          Date endDate = MyDateUtil.stringToDate(end, MyDateUtil.TYPE_MINITE);
          boolean flag = startDate != null && startDate.before(endDate);

          if (!flag) {
            ToastUtil.show(R.string.reachTimeBeforeStartTime);
          } else {
            volleyPostSave();
          }
        } else {
          volleyPostSave();
        }
        break;
      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  private void volleyPostSave() {

    String url = ApiConst.getAbsoluteUrl(ApiConst.URL_TRAVEL_LIST_SAVE);

    HashMap<String, String> params = new HashMap<>();
    //		// 更新content

    mTravelListContent.setLEAVE_TIME(etstartDate.getText().toString());
    mTravelListContent.setLEAVE_PLACE(etstartEar.getText().toString());
    mTravelListContent.setARRIVE_TIME(etendDate.getText().toString());
    mTravelListContent.setARRIVE_PLACE(etendEar.getText().toString());

    // 连网获取交通工具
    if (!TextUtils.isEmpty(mId)) {
      mTravelListContent.setTRAFTOOL(tvtraftool.getText().toString());//连网获取交通工具
      mTravelListContent.setTRAFTOOLID(mId);//连网获取交通工具的id
    }

    mTravelListContent.setTRAFTOOL_COST(ettraftoolCost.getText().toString());
    mTravelListContent.setTRAVEL_OFFSET(tvallStandard.getText().toString());
    mTravelListContent.setGRANT_STANDARD(etstandard.getText().toString());

    mTravelListContent.setCITY_TRAFIC_FEE(etcityCost.getText().toString());
    mTravelListContent.setTOFFSET_DAYS(ettravelDays.getText().toString());
    mTravelListContent.setTOOL_OFFSET(etnbuyCost.getText().toString());
    mTravelListContent.setHOTEL_FEE(etliveCost.getText().toString());
    mTravelListContent.setOTHER_FEE(etotherCost.getText().toString());

    mTravelListContent.setTCOST_BILLNUM(etPages.getText().toString());
    mTravelListContent.setCOST_SUBTOTAL(tvbillFee.getText().toString());
    mTravelListContent.setTICKETS_NUM(etcertificateNo.getText().toString());
    mTravelListContent.setNOTE(etNote.getText().toString());
    mTravelListContent.setSUM_BILL_FEE(tvallCost.getText().toString());

    params.put("json", new Gson().toJson(mTravelListContent));
    LogUtil.i(new Gson().toJson(mTravelListContent));
    params.put("regJobNo ", mCurrentUser.getJobNumber());//申请人工号
    params.put("mainId", mTravelListContent.getMAINID());//主表id

    MyUtils.showDialogLoading(getActivity());
    mActivity.volleyPost(url, params, new VolleyResponse() {

      @Override public void onVolleyResponse(String arg0) throws JSONException {

        MyUtils.dismissDialog();
        SuccessMessage[] result = new Gson().fromJson(arg0, SuccessMessage[].class);
        SuccessMessage workProcess = result[0];
        if (null == workProcess || null == workProcess.getResult()) {
          Toast.makeText(getActivity(), R.string.failure_operation, Toast.LENGTH_SHORT).show();
          return;
        }
        String successType = workProcess.getResult();
        if (successType.equals(SuccessMessage.FAILURE)) {
          Toast.makeText(getActivity(), R.string.failure_operation, Toast.LENGTH_SHORT).show();
          return;
        }
        if (successType.equals(SuccessMessage.SUCCESS)) {
          Toast.makeText(getActivity(), R.string.success_operation, Toast.LENGTH_SHORT).show();
          getActivity().setResult(Activity.RESULT_OK);
          getActivity().finish();
        }
      }

      @Override public void onVolleyError(VolleyError arg0) {
        // Auto-generated method stub

      }
    });
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) return;
    super.onActivityResult(requestCode, resultCode, data);
  }

  public static EditTravelReimburseListDetailFragment newInstance(
      TravelListContent mTravelListContent) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(EXTRA_TRAVELLIST_CONTENT, mTravelListContent);
    EditTravelReimburseListDetailFragment fragment = new EditTravelReimburseListDetailFragment();
    fragment.setArguments(bundle);
    return fragment;
  }
}
